﻿// 
//    Copyright 2010 The JaernCloud Project
// 
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
// 
//        http://www.apache.org/licenses/LICENSE-2.0
// 
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JaernCloud.Language.Compiler;
using System.Globalization;

namespace JaernCloud.Language.Compiler.Ast {
    public static class AstUtils {

        public static void ApplyConstant(AstConstantOperator op, AstConstantValueKind kind, AstConstant constant, AstNode node, Token t) {
            switch (op) {
                case AstConstantOperator.InheritsFrom:
                    InheritsFrom(op, kind, constant, node, t);
                    break;
                case AstConstantOperator.Value:
                    Value(op, kind, constant, node, t);
                    break;
            }
        }

        public static void Value(AstConstantOperator op, AstConstantValueKind kind, AstConstant constant, AstNode node, Token t) {
            switch (kind) {
                case AstConstantValueKind.String:
                    node.Constant = ParseToken<string>(t, kind);
                    break;
                case AstConstantValueKind.Integer:
                    node.Constant = ParseToken<int>(t, kind);
                    break;
                case AstConstantValueKind.Float:
                    node.Constant = ParseToken<float>(t, kind);
                    break;
                case AstConstantValueKind.Double:
                    node.Constant = ParseToken<double>(t, kind);
                    break;
                case AstConstantValueKind.Constant:
                    switch (constant) {
                        case AstConstant.False:
                            node.Constant = false;
                            break;
                        case AstConstant.True:
                            node.Constant = true;
                            break;
                        case AstConstant.Null:
                            node.Constant = AstConstant.Null;
                            break;
                    }
                    break;
            }
        }

        public static void InheritsFrom(AstConstantOperator op, AstConstantValueKind kind, AstConstant constant, AstNode node, Token t) {
            AstInheritsFrom inheritsFrom = new AstInheritsFrom(kind);
            switch (kind) {
                case AstConstantValueKind.String:
                    inheritsFrom.StringValue = ParseToken<string>(t, kind);
                    break;
                case AstConstantValueKind.Integer:
                    inheritsFrom.IntegerValue = ParseToken<int>(t, kind);
                    break;
                case AstConstantValueKind.Float:
                    inheritsFrom.FloatValue = ParseToken<float>(t, kind);
                    break;
                case AstConstantValueKind.Double:
                    inheritsFrom.DoubleValue = ParseToken<double>(t, kind);
                    break;
                case AstConstantValueKind.Constant:
                    inheritsFrom.Constant = constant;
                    break;
            }
            node.InheritsFrom.Add(inheritsFrom);
        }

        public static T ParseToken<T>(Token t, AstConstantValueKind kind) {
            switch (kind) {
                case AstConstantValueKind.String:
                    return (T)((object)t.val.Trim('"'));
                case AstConstantValueKind.Integer:
                    return (T)((object)Int32.Parse(t.val));
                case AstConstantValueKind.Float:
                    return (T)((object)Single.Parse(t.val, CultureInfo.InvariantCulture));
                case AstConstantValueKind.Double:
                    return (T)((object)Double.Parse(t.val, CultureInfo.InvariantCulture));
                default:
                    throw new ArgumentException(kind + " is not parseable");
            }
        }

    }
}
